package com.example.backend.controller;

import com.example.backend.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Cookie;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/logistics")
public class LogisticsController {
    /**
     * 物流
     * 注册《，登录《，提交资质《，修改密码《，提交物流信息《
     */
    final static int accountType = 2;

    @Autowired
    private LogisticsService logisticsService;

    //注册
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody Map<String, String> credentials) {
        String username = credentials.get("username");
        String passWordF = credentials.get("passWordF");
        String passwordS = credentials.get("passwordS");
        String qualificationContent = credentials.get("qualificationContent");
        Map<String, Object> resp = new HashMap<>();
        //判断密码是否为空
        if (username == null || passWordF == null || passwordS == null || qualificationContent == null) {
            resp.put("message", "输入不能为空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(resp);
        }

        if (!passwordS.equals(passWordF)) {
            resp.put("message", "密码不一致");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(resp);
        }

        String result = logisticsService.register(username, passWordF, passwordS, qualificationContent, accountType);
        if ("用户已存在".equals(result)) {
            resp.put("message", "用户已存在");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(resp);
        }

        if ("注册成功".equals( result)) {
            resp.put("message", result);
            return ResponseEntity.ok(resp);
        }
        resp.put("message", "未知错误");
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(resp);
    }

    //登录
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> credentials, HttpServletResponse response) {
        String accountId = credentials.get("accountId");
        String password = credentials.get("password");

        Map<String, Object> resp = new HashMap<>();
        // 验证必要参数
        if (accountId == null || password == null) {
            resp.put("message", "accountId and password are required");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(resp);
        }

        // 调用登录服务
        String result = logisticsService.login(accountId, password, accountType);

        if ("登录成功".equals(result)) {
            // 登录成功，设置Cookie
            Cookie accountCookie = new Cookie("accountId", accountId);
            accountCookie.setMaxAge(24 * 60 * 60); // 24小时
            accountCookie.setPath("/");
            response.addCookie(accountCookie);
            
            // 返回成功信息
            resp.put("message", result);
            resp.put("accountId", accountId);
            return ResponseEntity.ok(resp);
        } else {
            // 登录失败
            resp.put("message", result);
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(resp);
        }
    }
    
    //修改密码
    @PostMapping("/changePassword")
    public ResponseEntity<Map<String, Object>> changePassword(@RequestBody Map<String, String> credentials) {
        String accountId = credentials.get("accountId");
        String oldPassword = credentials.get("oldPassword");
        String newPassword = credentials.get("newPassword");

        Map<String, Object> resp = new HashMap<>();
        // 验证必要参数
        if (accountId == null || oldPassword == null || newPassword == null) {
            resp.put("message", "accountId, oldPassword and newPassword are required");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(resp);
        }

        // 调用修改密码服务
        String result = logisticsService.changePassword(accountId, oldPassword, newPassword, accountType);

        resp.put("message", result);
        if ("修改密码成功".equals(result)) {
            // 修改成功，返回成功信息
            return ResponseEntity.ok(resp);
        } else {
            // 修改失败
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(resp);
        }
    }
    
    //提交资质
    @PostMapping("/postQualificationContent")
    public ResponseEntity<Map<String, Object>> postQualificationContent(@RequestBody Map<String, String> credentials) {
        String accountId = credentials.get("accountId");
        String qualificationContent = credentials.get("qualificationContent");

        Map<String, Object> resp = new HashMap<>();
        // 验证必要参数
        if (accountId == null || qualificationContent == null) {
            resp.put("message", "参数为空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(resp);
        }

        // 调用提交资质服务
        String result = logisticsService.postQualificationContent(accountId, qualificationContent, accountType);

        resp.put("message", result);
        if ("提交资质成功".equals(result)) {
            // 提交成功，返回成功信息
            return ResponseEntity.ok(resp);
        } else {
            // 提交失败
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(resp);
        }
    }

    //提交物流信息
    @PostMapping("/postLogisticsInfo")
    public ResponseEntity<Map<String, Object>> postLogisticsInfo(@RequestBody Map<String, String> credentials) {
        Map<String, Object> resp = new HashMap<>();
        String result = logisticsService.postLogisticsInfo(credentials);
        resp.put("message", result);
        if ("提交物流信息成功".equals(result)){
            return ResponseEntity.ok(resp);
        }
        resp.put("message", result);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resp);
    }
}